package com.aivin.lib_englishlearn.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.aivin.lib_base.myprovider.ILoginProvider;
import com.aivin.lib_base.tool.RouteUtils;
import com.aivin.lib_base.tool.WkLog;
import com.aivin.lib_widget.TextViewVertical;
import com.aivin.lib_englishlearn.R;
import com.aivin.lib_englishlearn.adapter.EnglishAdapter;
import com.aivin.lib_englishlearn.bean.EnglishData;
import com.aivin.lib_widget.base.BaseActivity;
import com.aivin.lib_widget.bean.MarginDecoration;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.aivin.lib_base.config.ProviderConfig;
import com.wk.bmoblib.tool.DataTool;

import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

@Route(path = RouteUtils.EnglishListActivity)
public class EnglishListActivity extends BaseActivity {
    private TextViewVertical infoTv;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.libenglish_activity_main);

        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.color_2e4a7e)
                .init();

        activity = this;
        initView();
        DataTool.getInstance().getEnglishList(findListenerBook);
        testArouterService() ;
        testIprover();
        /*new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                WkLog.showErrorLog("开始....2");
                testIprover();
            }
        } ,2*1000) ;
        WkLog.showErrorLog("开始....1");*/


    }


    private void testArouterService() {
        /*BmoDtateServiceImpl service =  (BmoDtateServiceImpl) ARouter.getInstance().build(ProviderConfig.bmoDtateService).navigation();
        String result = service.sayHello("aivin") ;
        WkLog.showLog("result--->"+result);*/
    }



    private void testIprover(){
        ILoginProvider service =  (ILoginProvider) ARouter.getInstance().build(ProviderConfig.LoginInfoProvider).navigation();
        boolean isLogined = service.isLogined( ) ;
        WkLog.showLog("isLogined="+isLogined);
    }


    private RecyclerView recyclerView;
    private void initView() {

        infoTv = findViewById(R.id.infoTv);
        infoTv.setText("加载中...");
        recyclerView = findViewById(R.id.recycle);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        recyclerView.addItemDecoration(new MarginDecoration(this));

        SwipeRefreshLayout swipeRefreshLay = findViewById(R.id.swipeRefreshLay);
        swipeRefreshLay.setEnabled(false);

        findViewById(R.id.exitActivityBtn).setOnClickListener(v -> activity.finish());
    }


    private FindListener<EnglishData> findListenerBook = new FindListener<EnglishData>() {
        @Override
        public void done(List<EnglishData> datas, BmobException e) {
            if (datas != null && datas.size() > 0) {
                infoTv.setText("︵" + "英文" + "︶");
                updateChapters(datas);
            } else {
                infoTv.setText("读取失败");
            }
            if (e != null) {

                WkLog.showLog("bmob 返回错误==" + e.getMessage());
            }
        }
    };

    private void updateChapters(List<EnglishData> apps) {
        recyclerView.setAdapter(new EnglishAdapter(apps, (obj, itemIndex) -> {
            if (obj instanceof EnglishData) {
                toGoNewActivity((EnglishData) obj);
            }
        }
        ));
    }


    private void toGoNewActivity(EnglishData bean) {
        if (bean == null) {
            toastMsg("bean is null");
            return;
        }


        ARouter.getInstance().build(RouteUtils.EnglishLearningActivity)
                .withString("mediaUrl", bean.getArticleMediaUrl() == null ? "noMedia" : bean.getArticleMediaUrl().getUrl())
                .withString("articleContent", bean.getArticleContent())
                .withString("objectId", bean.getObjectId())
                .navigation();

    }

}
